package com.sy.service.impl;

import com.github.pagehelper.PageHelper;
import com.sy.dto.StudentClassTreeResult;
import com.sy.entity.Student;
import com.sy.exception.BusinessException;
import com.sy.mapper.StudentMapper;
import com.sy.service.StudentService;
import com.sy.vo.StudentPagingVo;
import com.sy.vo.StudentVo;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.sy.constant.BusinessConstant.STUDENT_ID;
import static com.sy.constant.RegexConstant.STRONG_PWD_REGEX;


/**
 * @Author:fma
 * @Date: 2021/10/11 19:01
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public boolean editStuBySno(StudentVo studentVo) {
        //学生学号
        Integer sno = studentVo.getSno();
        //学生新密码
        String pwd = studentVo.getPwd();
        //学生原密码
        String oldpwd = studentVo.getOldpwd();

        if (StringUtils.isBlank(oldpwd)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "原密码不能为空");
        }
        if (StringUtils.isBlank(pwd)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "新密码不能为空");
        }
        //原密码必须是强类型
        if (!pwd.matches(STRONG_PWD_REGEX)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "密码必须是强密码类型");
        }
        //根据学生sno查询该学生
        Student stuPwdBySno = getStuPwdBySno(sno);
        String pwd1 = stuPwdBySno.getPwd();
        if (!StringUtils.equals(oldpwd, pwd1)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "密码错误...");
        }
        //新密码必须是强类型
        if (!oldpwd.matches(STRONG_PWD_REGEX)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "密码必须是强密码类型");
        }
        if (StringUtils.equals(pwd, pwd1)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "近期密码不能相同...");
        }
        boolean r = studentMapper.fixStuBySno(studentVo);
        if (r) {
            return true;
        } else {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "修改失败");
        }

    }

    @Override
    public Student getStuPwdBySno(Integer sno) {
        if (Objects.isNull(sno)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "学生学号不能为空");
        }
        Student student = studentMapper.getStuPwdByName(sno);
        if (Objects.isNull(student)) {
            throw new BusinessException(HttpStatus.NOT_FOUND, "该学生未找到...");
        }
        return student;
    }

    @Override
    public Integer getStudentsSize() {
        List<Student> students = studentMapper.queryAllStudents();
        return students.size();
    }

    @Override
    public List<Student> queryAllStudents(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        List<Student> students = studentMapper.queryAllStudents();
        return students;
    }

    @Override
    public List<StudentClassTreeResult> getGradeAndClassName() {
        ArrayList<StudentClassTreeResult> studentClassTreeResults = new ArrayList<>();
        List<String> allGrade = studentMapper.getAllGrade();
        for (String s : allGrade) {
            StudentClassTreeResult studentClassTreeResult = new StudentClassTreeResult();
            studentClassTreeResult.setLabel(s);

            List<String> classNameByGrade = studentMapper.getClassNameByGrade(s);
            ArrayList<StudentClassTreeResult> studentClassTreeResults2 = new ArrayList<>();
            for (String s2 : classNameByGrade) {
                StudentClassTreeResult studentClassTreeResult2 = new StudentClassTreeResult();
                studentClassTreeResult2.setLabel(s2);
                studentClassTreeResults2.add(studentClassTreeResult2);
            }
            studentClassTreeResult.setChildren(studentClassTreeResults2);
            studentClassTreeResults.add(studentClassTreeResult);
        }
        return studentClassTreeResults;
    }

    @Override
    public Integer getStudentsNumByGradeAndClassName(String grade, String className) {
        List<Student> students = studentMapper.queryStudentsByGradeAndClassName(grade, className);
        if (students.isEmpty()) {
            throw new BusinessException(HttpStatus.NOT_FOUND, "班级不存在");
        }
        return students.size();
    }

    @Override
    public List<Student> queryStudentsByGradeAndClassName(StudentPagingVo studentPagingVo) {

        PageHelper.startPage(studentPagingVo.getPageNum(), studentPagingVo.getPageSize());
        List<Student> students = studentMapper.queryStudentsByGradeAndClassName(studentPagingVo.getGrade(), studentPagingVo.getClassName());
        if (students.isEmpty()) {
            throw new BusinessException(HttpStatus.NOT_FOUND, "班级不存在");
        }
        return students;
    }

    @Override
    public Integer getStudentsNumByStudentName(String studentName) {
        List<Student> students = studentMapper.queryStudentsByStudentName(studentName);
        if (students.isEmpty()) {
            throw new BusinessException(HttpStatus.NOT_FOUND, "学生姓名不存在");
        }
        return students.size();
    }

    @Override
    public List<Student> queryStudentsByStudentName(StudentPagingVo studentPagingVo) {
        PageHelper.startPage(studentPagingVo.getPageNum(), studentPagingVo.getPageSize());
        List<Student> students = studentMapper.queryStudentsByStudentName(studentPagingVo.getStudentName());
        if (students.isEmpty()) {
            throw new BusinessException(HttpStatus.NOT_FOUND, "学生姓名不存在");
        }
        return students;
    }

    @Override
    public Boolean redactStudent(Student student) {
        Boolean aBoolean = studentMapper.redactStudent(student);
        if (!aBoolean) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "修改学生信息失败...");
        }
        return aBoolean;
    }

    @Override
    public Boolean addStudents(Student student) {
        Boolean aBoolean = studentMapper.addInformationExceptStudentId(student);
        if (!aBoolean) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "添加学生信息失败...");
        }
        Boolean aBooleanStudentId = studentMapper.addStudentId(student.getId() + STUDENT_ID, student.getId());
        if (!aBooleanStudentId) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "添加学生学号失败...");
        }
        return aBoolean && aBooleanStudentId;
    }

    @Override
    public Boolean delStudentById(Integer id) {
        Boolean aBoolean = studentMapper.delStudentById(id);
        if (!aBoolean) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "删除学生信息失败...");
        }
        return aBoolean;
    }


}
